package pcsd.server;

import pcsd.worker.JobProcessingWorker;
import pcsd.worker.SimpleJobProcessingWorker;

/**
 * Runs the simple server.
 * 
 * @author Benjamin Ertl, Justinas Murzinas, Boyan Mihaylov
 */
public class TestJobQueueServerRunner {
	
	/**
	 * Test the server. For the sake of testing the interface, we
	 * do not use RMI but rather instantiate both the server and the workers
	 * locally and call the required methods.
	 */
	public static void main(String[] args) {
		try {
			/**
			 * Initialization
			 */
			int numWorkers = 1;
			int maxDequeSize = 10;
			
			JobQueueServer server = 
					new SimpleJobQueueServer(numWorkers, maxDequeSize);
			System.out.println("Server bound. Workers: " + numWorkers +
					", Max Deque Size: " + maxDequeSize);            

            /**
             * Testing
             */
			// ENQUEUE_TASK should return an error if the workers are not spawned
			System.out.print("Test: ENQUEUE_TASK fail, because workers are not spawned...");
			
			int result = server.enqueueTask(null);
			if (result == SimpleJobQueueServer.WORKER_THREADS_NOT_SPAWNED) {
				System.out.println("success");
			}
			else {
				System.out.println("fail");
			}
			
			// SPAWN_WORKER_THREADS should completely successfully
			System.out.print("SPAWN_WORKER_THREADS should completely successfully...");
			
			Monitor monitor = new Monitor();
			JobProcessingWorker[] workers = new JobProcessingWorker[1];
			workers[0] = new SimpleJobProcessingWorker(1);
			
			result = server.spawnWorkerThreads(workers, monitor);
			if (result == 0) {
				System.out.println("success");
			}
			else {
				System.out.println("fail");
			}
			
			// SPAWN_WORKER_THREADS should return error on second call
			System.out.print("SPAWN_WORKER_THREADS should return error on second call...");
			
			result = server.spawnWorkerThreads(workers, monitor);
			if (result == SimpleJobQueueServer.WORKER_THREADS_ALREADY_SPAWNED) {
				System.out.println("success");
			}
			else {
				System.out.println("fail");
			}
		} 
		catch (Exception e) {
			System.err.println("TestJobQueueServerRunner exception:");
			e.printStackTrace();
		}
	}
}
